import random
from django.http import HttpResponse
from django.http import JsonResponse
from django_redis import get_redis_connection
from celery_tasks.send_sms.sms_sender import sms_sender

from meiduo_mall.libs.yuntongxun.sms import CCP
from meiduo_mall.utils.response_code import RET
from . import constants

# Create your views here.
from django.views import View
from meiduo_mall.libs.captcha.captcha import captcha


class ImageCodeView(View):
    def get(self, request, image_code_id):
        # 生成图片验证码
        text, image_data = captcha.generate_captcha()
        print(text)

        # 把验证码数据保存到redis
        image_code_id = "image_code_" + image_code_id
        # print(image_code_id, image_data)
        redis_conn = get_redis_connection("code")
        # print(text)
        redis_conn.setex(name=image_code_id, time=constants.REDIS_IMAGE_CODE_EXPIRE, value=text)

        # 图片验证码返回到img的请求
        return HttpResponse(image_data, content_type='image/png')


class SmsCodeView(View):
    def get(self, request, mobile):
        image_code = request.GET.get("image_code")
        image_code_id = request.GET.get("image_code_id")
        # print(image_code, image_code_id)

        # 判断验证码是否有误
        # 判断是否为空
        if not all([image_code, image_code_id]):
            return JsonResponse({"code":RET.PARAMERR, "errmsg":"参数不全"})

        # 与数据库的数据进行比对
        redis_conn = get_redis_connection("code")
        redis_image_code = redis_conn.get("image_code_%s" % image_code_id)
        # 删除验证码
        redis_conn.delete("image_code_%s" % image_code_id)

        if not redis_image_code:
            return JsonResponse({"code":RET.NODATA, "errmsg":"验证码过期了"})

        if not image_code.lower() == redis_image_code.decode().lower():
            return JsonResponse({"code":RET.DATAERR, "errmsg":"图片验证码输入有误"})

        # 验证前后两次发送短信时间间隔是否大于60秒
        if redis_conn.get("send_flag_%s" % mobile):
            return JsonResponse({"code":RET.SMSCODERR, "errmsg":"发送短信太频繁"})

        # 发送短信
        sms_code = "%06d" % random.randint(0, 999999)
        print(sms_code)

        # 用celery去发送短信
        sms_sender.delay(mobile, sms_code, constants.REDIS_SMS_CODE_EXPIRES/60)

        # 用pipe方法（类似于事务）
        pipeline = redis_conn.pipeline()

        pipeline.setex("sms_code_%s" % mobile, constants.REDIS_SMS_CODE_EXPIRES, sms_code)

        # 把时间间隔写入数据库中
        pipeline.setex("send_flag_%s" % mobile, constants.REDIS_SEND_FLAG_EXPIRES, 1)

        pipeline.execute()

        return JsonResponse({"code":RET.OK, "errmsg":"发送成功"})


class PasswordSmsCodeView(View):
    def get(self, request):

        mobile = request.GET.get("mobile")

        redis_conn = get_redis_connection("code")

        # 验证前后两次发送短信时间间隔是否大于60秒
        if redis_conn.get("send_flag_%s" % mobile):
            return JsonResponse({"code": RET.SMSCODERR, "errmsg": "发送短信太频繁"})

        # 发送短信
        sms_code = "%06d" % random.randint(0, 999999)
        print(sms_code)

        # 用celery去发送短信
        sms_sender.delay(mobile, sms_code, constants.REDIS_SMS_CODE_EXPIRES / 60)

        # 用pipe方法（类似于事务）
        pipeline = redis_conn.pipeline()

        pipeline.setex("sms_code_%s" % mobile, constants.REDIS_SMS_CODE_EXPIRES, sms_code)

        # 把时间间隔写入数据库中
        pipeline.setex("send_flag_%s" % mobile, constants.REDIS_SEND_FLAG_EXPIRES, 1)

        pipeline.execute()

        return JsonResponse({"code": RET.OK, "errmsg": "发送成功"})